package leetcode_day._2021._202104._1120;

import java.util.Arrays;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/4/12 19:54
 * 最大数
 * 思路：
 *   排序
 */
public class _12_179 {
    public static void main(String[] args) {
        int[] nums = {0, 0};
        System.out.println(new _12_179().largestNumber(nums));
        Integer[] integers = {10, 2};
        Arrays.sort(integers, (x, y) -> {
            long num1 = Long.parseLong(x + "" + y), num2 = Long.parseLong(x + "" + y);
            return -(int) (num1 - num2);
        });
        System.out.println(Arrays.toString(integers));
    }

    public String largestNumber(int[] nums) {
        // 使用比较器要使用包装类
        Integer[] integers = new Integer[nums.length];
        for (int i = 0; i < nums.length; i++) {
            integers[i] = nums[i];
        }
        Arrays.sort(integers, (x, y) -> {
            long num1 = Long.parseLong(x + "" + y), num2 = Long.parseLong(x + "" + y);
            return -(int) (num1 - num2);
        });
//        for (int i = 1; i <= nums.length - 1; i++) {
//            for (int j = nums.length - 1; j >= i; j--) {
//                int flag = myCompare(nums[j] + "", nums[j - 1] + "");
//                if (flag == 1) {
//                    int tem = nums[j];
//                    nums[j] = nums[j - 1];
//                    nums[j - 1] = tem;
//                }
//            }
//        }
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < nums.length; ++i) {
            if (nums[i] == 0 && i != 0 && '0' == ans.charAt(0)) break;
            ans.append(nums[i]);
        }
        return ans.toString();
    }
    public int myCompare(String num1, String num2) {
        long x = Long.parseLong(num1 + num2), y = Long.parseLong(num2 + num1);
        return x >= y ? 1 : -1;
    }
//    public int myCompare(String num1, String num2) {
//        int min = Math.min(num1.length(), num2.length());
//        for (int i = 0; i < min; i++) {
//            int n1 = (int) num1.charAt(i), n2 = (int) num2.charAt(i);
//            if (n1 > n2) return 1;
//            else if (n1 < n2) return -1;
//        }
//        if (num1.length() == num2.length()) return 0;
//        else if (num1.length() < num2.length()) return num1.charAt(min - 1) > num2.charAt(min) ? 1 : -1;
//        else return num1.charAt(min) > num2.charAt(min - 1) ? 1 : -1;
//    }
}
